package GUILayer;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.ListModel;

import ControlLayer.*;
import GUILayer.OrdreModel;
import GUILayer.SalgsVareModel;
import ModelLayer.LagerVare;
import ModelLayer.Ordre;
import ModelLayer.Produkt;

import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import java.awt.TextArea;

public class PakOrdreGUI extends JFrame {
	
	private JPanel contentPane;
	private JTable ordreTable;
	private JTable salgsVareTable;
	private JTextField txtFindOrdre;
	private int id;
	private JTable lagerVareTable;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PakOrdreGUI frame = new PakOrdreGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PakOrdreGUI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 550);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmAfslut = new JMenuItem("Afslut");
		mnFile.add(mntmAfslut);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(189, 64, 693, 651);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 450, 169);
		panel.add(scrollPane);
		
		ordreTable = new JTable();
		scrollPane.setViewportView(ordreTable);
		ordreTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				visSalgsVare();
			}
		});
		ordreTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ordreTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"KundeID", "OrdreID", "OrdreStatus"
			}
		));
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 240, 285, 151);
		panel.add(scrollPane_1);
		
		salgsVareTable = new JTable();
		scrollPane_1.setViewportView(salgsVareTable);
		salgsVareTable.addMouseListener(new MouseAdapter() {
	
			public void mouseClicked(MouseEvent arg0) {
				visLager();
			}
		});
		salgsVareTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		salgsVareTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
				{null, null, null},
			},
			new String[] {
				"ProduktID", "Antal"
			}
		));
		
		JMenuItem mntmVarerPOrdren = new JMenuItem("Varer p\u00E5 valgt ordre");
		mntmVarerPOrdren.setBounds(10, 193, 285, 36);
		panel.add(mntmVarerPOrdren);
		mntmVarerPOrdren.setFont(new Font("Segoe UI", Font.PLAIN, 26));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(270, 213, 308, 309);
		panel.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(33, 27, 156, 152);
		panel_1.add(scrollPane_2);
		
		lagerVareTable = new JTable();
		scrollPane_2.setViewportView(lagerVareTable);
		lagerVareTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lagerVareTable.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
				{null, null},
			},
			new String[] {
				"Lager Nummer"
			}
		));
		
		JButton btnVisPakke1 = new JButton("Vis modtagne ordrer");
		btnVisPakke1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				visPakke1();
			}
		});
		btnVisPakke1.setBounds(9, 77, 170, 23);
		contentPane.add(btnVisPakke1);
		
		JButton btnOpdatereOrdre = new JButton("Opdater markeret ordre");
		btnOpdatereOrdre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				opdatereStatus();
			}
		});
		btnOpdatereOrdre.setBounds(9, 111, 170, 23);
		contentPane.add(btnOpdatereOrdre);
		
		JButton btnFindOrdre = new JButton("Find ordre");
		btnFindOrdre.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
					findOrdre();
						
			}
		});
		btnFindOrdre.setBounds(9, 173, 170, 23);
		contentPane.add(btnFindOrdre);
		
		txtFindOrdre = new JTextField();
		txtFindOrdre.setText("Indtast ordreID");
		txtFindOrdre.setBounds(9, 145, 170, 20);
		contentPane.add(txtFindOrdre);
		txtFindOrdre.setColumns(1);
		
		JMenuItem mntmInformationOmOrdrer = new JMenuItem("Oversigt over ordrer");
		mntmInformationOmOrdrer.setFont(new Font("Segoe UI", Font.PLAIN, 34));
		mntmInformationOmOrdrer.setBounds(199, 11, 347, 42);
		contentPane.add(mntmInformationOmOrdrer);
	}
	
	private void visPakke1()
	{
		OrdreCtr oCtr = new OrdreCtr();
		
		OrdreModel trackModelObj = new OrdreModel();
		
		 ArrayList<Ordre> ordreList = oCtr.visAlleOrdrePak1();
		 
				 
	     System.out.println(ordreList.size());
         trackModelObj.setData(ordreList);
         ordreTable.setModel(trackModelObj);
         this.setVisible(true);
	}
	
	private void visSalgsVare()
	{
		OrdreCtr oCtr = new OrdreCtr();
		SalgsVareModel trackModelObj = new SalgsVareModel();
		int row = ordreTable.getSelectedRow();
		int column = 1;
		int oId = (Integer) ordreTable.getValueAt(row, column);

		 ArrayList<ModelLayer.SalgsVare> salgsList = oCtr.visSalgsVare(oId);
	     System.out.println(salgsList.size());
         trackModelObj.setData(salgsList);
         salgsVareTable.setModel(trackModelObj);
         
         this.setVisible(true);
	}
	
	private void visLager()
	{
		LagerCtr lCtr = new LagerCtr();
		LagerVareModel trackModelObj = new LagerVareModel();
		
		int row = salgsVareTable.getSelectedRow();
		int column = 0;
		int nr = (Integer) ordreTable.getValueAt(row, column);
		
		ArrayList<LagerVare> lagerList = lCtr.visLagerNr(nr);
		
		trackModelObj.setData(lagerList);
        lagerVareTable.setModel(trackModelObj);
        this.setVisible(true);
		
		
	}

	private void findOrdre()
	{
		
	
		OrdreCtr oCtr = new OrdreCtr();
		OrdreModelSingle trackModelObj = new OrdreModelSingle();
		String s = txtFindOrdre.getText();
		try{
			id = Integer.parseInt(s);
			Ordre o = oCtr.findOrdre(id);
			
			trackModelObj.setData(o);
			
			ordreTable.setModel(trackModelObj);
			this.setVisible(true);
		}
		catch (Exception e)
		{
			txtFindOrdre.setText("Fejl id");
			this.setVisible(true);
		}
		
	}
	
	private void findStatus(int id)
	{		
		OrdreCtr oCtr = new OrdreCtr();
		OrdreModelSingle trackModelObj = new OrdreModelSingle();
		
			Ordre o = oCtr.findOrdre(id);	
			trackModelObj.setData(o);
			
			ordreTable.setModel(trackModelObj);
			this.setVisible(true);
		
		
	}
	

	private void opdatereStatus()
	{
		OrdreCtr oCtr = new OrdreCtr();
		int row = ordreTable.getSelectedRow();
		if(row != -1)
		{
			int statusColoumn = 2;
			int idColoumn = 1;
			
			String ordreStatus = (String) ordreTable.getValueAt(row, statusColoumn);
			int ordreId = (Integer) ordreTable.getValueAt(row, idColoumn);
			oCtr.opdatereStatus(ordreId, ordreStatus);
			findStatus(ordreId);
			
			
		}
		else {
			
		}
		
		
	}
}
